package com.zzx.threekingdom.activity;

import com.zzx.threekingdom.R;
import com.zzx.threekingdom.card.CardHeap;
import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.card.CharacterCard;
import com.zzx.threekingdom.judge.Judge.IAskYesNo;
import com.zzx.threekingdom.judge.Rule;
import com.zzx.threekingdom.judge.Judge;
import com.zzx.threekingdom.judge.Judge.IPassCard;
import com.zzx.threekingdom.judge.Judge.IPassCards;
import com.zzx.threekingdom.judge.Rule.CardRule;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.UserPlayerModel;
import com.zzx.threekingdom.view.CardHeapView;
import com.zzx.threekingdom.view.CardPickerView;
import com.zzx.threekingdom.view.CardView;
import com.zzx.threekingdom.view.HintToast;
import com.zzx.threekingdom.view.LogTextBox;
import com.zzx.threekingdom.view.OtherPlayersView;
import com.zzx.threekingdom.view.PickCardDialog;
import com.zzx.threekingdom.view.PlayerCardView;
import com.zzx.threekingdom.view.PlayerView;
import com.zzx.threekingdom.view.OtherPlayersView.SelectListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;



public class PlayActivity extends Activity implements View.OnClickListener, PlayerCardView.SelectCardsListener, SelectListener
{
    public enum DisplayMode
    {
        CanDoNothing, InturnActiveUseCard, AbandonCards, SelectCards, AskYesNo
    };
    
    public enum SelectMode
    {
        ByName, ByNumber
    };
    
    private DisplayModeControl mCurrentContrl = null;
    
    
    private final static int MENU_STARTGAME = 0;
    
    public DisplayMode mDisplayMode = DisplayMode.CanDoNothing;
    
    public PlayerView mPlayerView;
    public PlayerCardView mPlayerCardView;
    public OtherPlayersView mOtherPlayersView;

    //private Button menuButton;
    private Button startButton;
    private Button okButton;
    private Button cancelButton;
    private Button abandonButton;
    private CardHeapView cardHeapView;
    private UserPlayerModel userPlayerModel;
    private LogTextBox mLogTextBox;
    
    public IPassCards mCurrentPassCards = null;
    public IPassCard mCurrentPassCard = null;
    public IAskYesNo mCurrentAskYesNo = null;
    
    private MyHandler myHandler = new MyHandler();
    private HintToast mHintToast;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play);
        mPlayerView = (PlayerView)findViewById(R.id.PlayerView);
        mPlayerCardView = (PlayerCardView)findViewById(R.id.PlayerCardView);
        mPlayerCardView.setSelectListener(this);
        mOtherPlayersView = (OtherPlayersView)findViewById(R.id.OtherPlayersView);
        mOtherPlayersView.setSelcetListener(this);
        //menuButton = (Button)findViewById(R.id.ButtonMenu);
        //menuButton.setOnClickListener(this);
        startButton = (Button)findViewById(R.id.ButtonStart);
        startButton.setOnClickListener(this);
        okButton = (Button)findViewById(R.id.OK);
        okButton.setOnClickListener(this);
        abandonButton = (Button)findViewById(R.id.Abandon);
        abandonButton.setOnClickListener(this);
        cancelButton = (Button)findViewById(R.id.Cancel);
        cancelButton.setOnClickListener(this);
        cardHeapView = (CardHeapView)findViewById(R.id.CardHeapView);
        mLogTextBox = (LogTextBox)findViewById(R.id.LogTextBox);
        mHintToast = new HintToast(this);
        
        resetGame();
    }
    

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, MENU_STARTGAME, 1, "开始"); 
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int menuId = item.getItemId();
        if(menuId == MENU_STARTGAME)
        {
            startGame(2);
        }
        return true;
    }
    @Override
    public boolean onPrepareOptionsMenu (Menu menu)
    {
        menu.findItem(MENU_STARTGAME).setEnabled(Judge.judge.IsGameRunning()==false);
        return true;
    }
    @Override
    public void onBackPressed ()
    {
        AlertDialog.Builder b = new AlertDialog.Builder(this);
        b.setTitle("退出确认").setMessage("确定终止游戏？").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Judge.judge.clear();
                PlayActivity.this.finish();
            }
        }).setNegativeButton("取消", null).create().show();
    }
    
    public void resetGame()
    {
        mPlayerView.clearView();
        mPlayerCardView.clearView();
        mOtherPlayersView.clearView();
        mLogTextBox.clearView();
        
        Judge.reload();
        Judge.judge.setLogTextBox(mLogTextBox);
        setDisplayMode(DisplayMode.CanDoNothing);
        cardHeapView.setCards(CardHeap.cardHeap.playCards, CardHeap.cardHeap.trashPlayCards);
    }
    
    public void onClick(View view)
    {
        
        if(view==okButton)
            onOkButton();
        else if(view==cancelButton)
            onCancelButton();
        else if(view==abandonButton)
            onAbandonButton();
        else if(view==startButton)
            onStartButton();
        /*else if(view==menuButton)
            openOptionsMenu();*/
        
        
    }
    
    public void startGame(int num)
    {
        Judge.judge.startGame(this, mOtherPlayersView, num);
        AbsPlayerModel pm = Judge.judge.getModelByPlayerView(mPlayerView);
        if(pm instanceof UserPlayerModel)
            userPlayerModel = (UserPlayerModel)pm;
        
        endGame();
    }
    
    public AlertDialog endGame()
    {
        return (new AlertDialog.Builder(this)).setNeutralButton("确定"
            , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); }})
            .setTitle("游戏结束").create();
    }
    
    public void setHintText(String hint)
    {
        if(hint!=null&&hint.length()!=0)
            mHintToast.showHintText(this, hint, mPlayerCardView.getHeight());
        else
            mHintToast.hideHintText();
    }
    
    

    
    public void onOkButton()
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onOkButton();
    }
    public void onCancelButton()
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onCancelButton();
    }
    public void onAbandonButton()
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onAbandonButton();
    }
    public void onStartButton()
    {
        startGame(2);
        startButton.setEnabled(Judge.judge.IsGameRunning()==false);
    }
    public void onSelectCard(CardList cards)
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onSelectCard(cards);
    }
    public void onSelectSingleCard(Card card)
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onSelectSingleCard(card);
    }
    public void onSelectCard(ArrayList<CardView> cards) {}
    
    public void onSelected(ArrayList<PlayerView> pvs)
    {
        if(mCurrentContrl!=null)
            mCurrentContrl.onSelected(pvs);
    }
    
    
    public void setDisplayModeInternal(DisplayMode displayMode)
    {
        mDisplayMode = displayMode;
        mCurrentContrl = getDMCByDisplayMode(displayMode);
        myHandler.post(new Runnable() { public void run() { mCurrentContrl.enterMode(); }});
        
    }
    public void setDisplayMode(DisplayMode displayMode)
    {
        mCurrentPassCards = null;
        mCurrentPassCard = null;
        mCurrentAskYesNo = null;
        setDisplayModeInternal(displayMode);
    }
    public void setDisplayMode(DisplayMode displayMode, IPassCard ipc, IPassCards ipcs)
    {
        mCurrentPassCards = ipcs;
        mCurrentPassCard = ipc;
        setDisplayModeInternal(displayMode);
    }
    
    public void setSelectCardsByNameDisplayMode(String name, IPassCard ipc)
    {
        mCurrentPassCard = ipc;
        selectCardsControl.setByNameMode(name);
        setDisplayModeInternal(DisplayMode.SelectCards);
    }
    
    public void setSelectCardsByNumberDisplayMode(int number, IPassCards ipcs)
    {
        mCurrentPassCards = ipcs;
        selectCardsControl.setByNumberMode(number);
        setDisplayModeInternal(DisplayMode.SelectCards);
    }
    
    public void setAskYesNoDisplayMode(IAskYesNo ia)
    {
    	mCurrentAskYesNo = ia;
    	setDisplayModeInternal(DisplayMode.AskYesNo);
    }
    
    public void pickOneCard(ArrayList<CardList> cardss, final IPassCard i)
    {
        PickCardDialog pcd = new PickCardDialog(this, true);
        pcd.setCardss(cardss).setCardSelectListener(new CardPickerView.onSelectCardListener()
        	{ public void onSelectCard(Card card) { i.onGotCard(card); }}).show();
    }

    
    public void pickOneCharacter(CardList characterCards, final IPassCard i)
    {
        PickCardDialog pcd = new PickCardDialog(this, true);
        pcd.setCardList(characterCards).setCardSelectListener(new CardPickerView.onSelectCardListener()
        {
            public void onSelectCard(Card card)
            {
                i.onGotCard(card);
            }
        }).show();
    }
    
    public void selectAbondonCards(int num, IPassCards i) { setDisplayMode(DisplayMode.AbandonCards, null, i); }
    public void selectCardByname(String name, IPassCard i) { setSelectCardsByNameDisplayMode(name, i); }
    public void setCharacter(CharacterCard card) { mPlayerView.setCharacter(card); }
    public void onReceiveCards(CardList playCards) { mPlayerCardView.addCards(playCards); }
    public void onRemoveCards(CardList playCards) { mPlayerCardView.removeCards(playCards); }
    public void onRemoveCard(Card card) { mPlayerCardView.removeCard(card); }
    
    private DisplayModeControl getDMCByDisplayMode(DisplayMode displayMode)
    {
        switch(displayMode)
        {
            case CanDoNothing: return doNothingControl;
            case InturnActiveUseCard: return activeUseCardControl;
            case AbandonCards: return selectAbadonControl;
            case SelectCards: return selectCardsControl;
            case AskYesNo: return askYesNoControl;
            default: return doNothingControl;
        }
    }
    
    private DisplayModeControl doNothingControl = new DisplayModeControl();
    
    private DisplayModeControl activeUseCardControl = new DisplayModeControl()
    {
        @Override
        public void enterMode()
        {
            super.enterMode();
            for(int count = 0;count < mPlayerCardView.getChildCount();count++)
            {
                CardView cardView = (CardView)(mPlayerCardView.getChildAt(count));
                cardView.setEnabled(Rule.CardRule.isCardActiveUse(cardView.getCard(), userPlayerModel)==true);
            }
            abandonButton.setEnabled(true);
        }
        @Override
        public void onOkButton()
        {
            userPlayerModel.activeUseCard(mPlayerCardView.getSingleSelectedCard()
                    , Judge.judge.Pv2Pm(mOtherPlayersView.getSelectedPm()), null);
            onCancelButton();
        }
        @Override
        public void onSelectSingleCard(Card card)
        {
            super.onSelectSingleCard(card);
            if(card==null)
            {
                onCancelButton();
                return;
            }
            cancelButton.setEnabled(true);
            
            if(Rule.CardRule.isCardActiveUseByNoPlayer(card, userPlayerModel)==true)
            {
                okButton.setEnabled(true);
            }
            else if(Rule.CardRule.isCardActiveUseByTargetPlayer(card, userPlayerModel)==true)
            {
                okButton.setEnabled(false);
                mOtherPlayersView.setSelectedMode(1, CardRule.getTargetByActionName(userPlayerModel, card.getName()));
            }
            
        }
        @Override
        public void onAbandonButton()
        {
            super.onAbandonButton();
            userPlayerModel.enterAbandonCards();
        }
        
        @Override
        public void onSelected(ArrayList<PlayerView> pvs)
        {
            Card card = mPlayerCardView.getSingleSelectedCard();
            if(card==null)
                onCancelButton();
            else if(Rule.CardRule.isCardActiveUseByTargetPlayer(card, userPlayerModel)==true)
            {
                if(Rule.CardRule.getCardActiveUseTargetNum(card)==pvs.size())
                    okButton.setEnabled(true); 
            }
            else
                onCancelButton();
            

        }

    };
    
    private DisplayModeControl selectAbadonControl = new DisplayModeControl()
    {
        @Override
        public void enterMode()
        {
            super.enterMode();
            mPlayerCardView.setSingleSelectMode(false, userPlayerModel.getShouldAbandonCardsNum());
            abandonButton.setEnabled(true);
            mPlayerCardView.setEnabledAll(true);
        }
        @Override
        public void onOkButton()
        {
            mCurrentPassCards.onGotCards(mPlayerCardView.getSelectedCards());
        };
        @Override
        public void onAbandonButton()
        {

            
            mCurrentPassCards.onGotCards(mPlayerCardView.getSelectedCards());
        }
        @Override
        public void onSelectCard(CardList cards)
        {
            int num = cards.size();
            okButton.setEnabled(num==userPlayerModel.getShouldAbandonCardsNum());
            cancelButton.setEnabled(num>0);
        }

    };
    
    private SelectCardsDisplayModeControl selectCardsControl = new SelectCardsDisplayModeControl();
    
    private class SelectCardsDisplayModeControl extends DisplayModeControl
    {
        private SelectMode mMode;
        private String mName;
        private int mNumber;
        
        public void setMode(SelectMode mode) { mMode = mode; }
        public void setByNameMode(String name) { mName = name; setMode(SelectMode.ByName);}
        public void setByNumberMode(int number) { mNumber = number; setMode(SelectMode.ByName);}
        
        @Override
        public void enterMode()
        {
            super.enterMode();
            if(mMode==SelectMode.ByName)
            {
                for(int count = 0;count < mPlayerCardView.getChildCount();count++)
                {
                    CardView cardView = (CardView)(mPlayerCardView.getChildAt(count));
                    cardView.setEnabled(cardView.getCard().getName().equals(mName)==true);
                }
            }
            else if(mMode==SelectMode.ByNumber)
            {
                if(mNumber>1)
                    mPlayerCardView.setSingleSelectMode(false, mNumber);
            }
            
            cancelButton.setEnabled(true);
        }
        
        @Override
        public void onOkButton()
        {
            if(mMode==SelectMode.ByName)
            {
                mCurrentPassCard.onGotCard(CardList.getFirstCard(mPlayerCardView.getSelectedCards()));
            }
            else if(mMode==SelectMode.ByNumber)
            {
                mCurrentPassCards.onGotCards(mPlayerCardView.getSelectedCards());
            }
        }
        
        @Override
        public void onCancelButton()
        {
            super.onCancelButton();
            onOkButton();
        }

        @Override
        public void onSelectCard(CardList cards)
        {
            int num = cards.size();
            if(mMode==SelectMode.ByName)
            {
                okButton.setEnabled(num!=0);
            }
            else if(mMode==SelectMode.ByNumber)
                okButton.setEnabled(num==mNumber);
        }
        

    };
    
    private AskYesNoDisplayModeControl askYesNoControl = new AskYesNoDisplayModeControl();
    private class AskYesNoDisplayModeControl extends DisplayModeControl
    {
        
        @Override
        public void enterMode()
        {
            super.enterMode();
            okButton.setEnabled(true);
            cancelButton.setEnabled(true);
        }
        
        @Override
        public void onOkButton()
        {
        }
        
        @Override
        public void onCancelButton()
        {
        }

    };
    

    
    private class DisplayModeControl
    {
        public void enterMode()
        {
            okButton.setEnabled(false);
            cancelButton.setEnabled(false);
            abandonButton.setEnabled(false);
            mPlayerCardView.setSingleSelectMode(true, -1);
            mPlayerCardView.setEnabledAll(false);
            mOtherPlayersView.setNoSelectMode();
        }
        public void onOkButton() {};
        public void onCancelButton()
        {
            mPlayerCardView.unselectAll();
            okButton.setEnabled(false);
            cancelButton.setEnabled(false);
            mOtherPlayersView.setNoSelectMode();
        };
        public void onAbandonButton() {};
        public void onSelectCard(CardList cards) {};
        public void onSelectSingleCard(Card card) {};
        public void onSelected(ArrayList<PlayerView> pvs) {};
    }
    
    private class MyHandler extends Handler
    {
        public MyHandler()
        {
            super();
        }
        
        public void handleMessage (Message msg)
        {
            
        }
    }


}
